<!--此页面用于放置页面的公共片段（fragment）-->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:fragment="static">
    <!-- css样式 -->
    <head>
        <style>
            body, html {
                margin: 0;
                padding: 0;
            }
        </style>
    </head>

    <!-- 引入公用库 -->
    <head>
        <script th:src="@{/jquery/jquery.js}"></script>

        <link th:href="@{/layui/css/layui.css}" rel="stylesheet" type="text/css"/>
        <script th:src="@{/layui/layui.js}"></script>

        <!--CryptoJS jsencrypt -->
        <script th:src="@{/js/cryptojs.js}"></script>
        <script th:src="@{/js/jsencrypt.js}"></script>
        <script th:src="@{/js/aesUtil.js}"></script>
        <script th:src="@{/js/rsaUtil.js}"></script>
    </head>

    <!-- 封装一些公用方法以及扩展方法 -->
    <head>
        <script>
            /**
             * 拓展表单对象：用于将对象序列化为JSON对象
             */
            $.fn.serializeObject = function () {
                var o = {};
                var a = this.serializeArray();
                $.each(a, function () {
                    if (o[this.name]) {
                        if (!o[this.name].push) {
                            o[this.name] = [o[this.name]];
                        }
                        o[this.name].push(this.value || '');
                    } else {
                        o[this.name] = this.value || '';
                    }
                });
                return o;
            };

            /**
             * 表单自动回显
             * 依赖jqury
             * 使用参考：$("#form1").form({"id":"112","username":"ff","password":"111","type":"admin"});
             */
            $.fn.form = function (data) {
                var form = $(this);
                for (var i in data) {
                    var name = i;
                    var value = data[i];
                    if (name !== "" && value !== "") {
                        valuAtion(name, value);
                    }
                }

                function valuAtion(name, value) {
                    if (form.length < 1) {
                        return;
                    }
                    if (form.find("[name='" + name + "']").length < 1) {
                        return;
                    }
                    var input = form.find("[name='" + name + "']")[0];
                    if ($.inArray(input.type, ["text", "password", "hidden", "select-one", "textarea"]) > -1) {
                        $(input).val(value);
                    } else if (input.type === "radio" || input.type === "checkbox") {
                        form.find("[name='" + name + "'][value='" + value + "']").attr("checked", true);
                    }
                }
            };

            /**
             * 拓展jQuery对象：快速AJAX Delete删除
             */
            $.delete = function (url, params, callback) {
                if (!params || typeof params === 'string') {
                    throw new Error('Error Params：' + params);
                }

                $.ajax({
                    url: url,
                    type: "DELETE",
                    contentType: 'application/json',//发送格式（JSON串）
                    data: JSON.stringify(params),//发送参数（JSON串）
                    success: function (result) {
                        callback && callback(result);
                    }
                });
            };

            /**
             * 常用工具方法
             */
            commonUtil = {
                /**
                 * 获取当前时间，并格式化输出为：2018-05-18 14:21:46
                 */
                getNowTime: function () {
                    var time = new Date();
                    var year = time.getFullYear();//获取年
                    var month = time.getMonth() + 1;//或者月
                    var day = time.getDate();//或者天

                    var hour = time.getHours();//获取小时
                    var minu = time.getMinutes();//获取分钟
                    var second = time.getSeconds();//或者秒
                    var data = year + "-";
                    if (month < 10) {
                        data += "0";
                    }
                    data += month + "-";
                    if (day < 10) {
                        data += "0"
                    }
                    data += day + " ";
                    if (hour < 10) {
                        data += "0"
                    }


                    data += hour + ":";
                    if (minu < 10) {
                        data += "0"
                    }
                    data += minu + ":";
                    if (second < 10) {
                        data += "0"
                    }
                    data += second;
                    return data;
                },

                /**
                 * 将我们响应的系统菜单数据转换成符合layui的tree结构
                 * @param arrar  旧数据
                 * @returns {Array} 新数据
                 */
                updateKeyForLayuiTree: function (arrar) {
                    let newArray = [];
                    for (let i = 0; i < arrar.length; i++) {
                        let obj1 = {};
                        let obj = arrar[i];
                        obj1.id = obj.menuId;
                        obj1.title = obj.menuName;
                        obj1.href = obj.menuPath;

                        if (obj.children.length > 0) {
                            obj1.children = this.updateKeyForLayuiTree(obj.children);
                        }
                        newArray.push(obj1);
                    }
                    return newArray
                },

                /**
                 * 在所有系统菜单上勾选用户菜单
                 * @param arrTree
                 * @param userTreeString
                 */
                checkedForLayuiTree:function (arrTree, userTreeString) {
                    for(let tree of arrTree){
                        //默认全部展开
                        tree.spread=true;
                        //递归子节点
                        if(tree.children && tree.children.length > 0){
                            tree.children = this.checkedForLayuiTree(tree.children,userTreeString);
                        }else{
                            //是否包含（勾选子节点默认会勾上父节点，如果勾选父节点，默认会全部勾上所有子节点）
                            if(userTreeString.search(tree.id) !== -1){
                                tree.checked = true;
                            }
                        }
                    }
                    return arrTree;
                }
            }
        </script>

        <!-- 重写ajax加密，并保留原始ajax，命名为_ajax -->
        <script>
            //获取前端RSA公钥密码、AES的key，并放到window
            let genKeyPair = rsaUtil.genKeyPair();
            window.jsPublicKey = genKeyPair.publicKey;
            window.jsPrivateKey = genKeyPair.privateKey;

            /**
             * 重写jquery的ajax方法
             */
            let _ajax = $.ajax;//首先备份下jquery的ajax方法
            $.ajax = function (opt) {
                //默认值
                // opt = {
                //     type: 'post',
                //     url: url,
                //     contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
                //     dataType: 'json',
                //     data: data,
                //     success: success,
                //     error: function (xhr, status, error) {
                //         console.log("ajax错误！");
                //     }
                // };

                //备份opt中error和success方法
                let fn = {
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                    },
                    success: function (data, textStatus) {
                    }
                };
                if (opt.error) {
                    fn.error = opt.error;
                }
                if (opt.success) {
                    fn.success = opt.success;
                }

                //加密再传输
                if (opt.type.toLowerCase() === "post") {
                    let data = opt.data;
                    //发送请求之前随机获取AES的key
                    let aesKey = aesUtil.genKey();
                    data = {
                        data: aesUtil.encrypt(data, aesKey),//AES加密后的数据
                        aesKey: rsaUtil.encrypt(aesKey, sessionStorage.getItem('javaPublicKey')),//后端RSA公钥加密后的AES的key
                        publicKey: window.jsPublicKey//前端公钥
                    };
                    opt.data = data;
                }

                //扩展增强处理
                let _opt = $.extend(opt, {
                    //成功回调方法增强处理
                    success: function (data, textStatus) {
                        if (opt.type.toLowerCase() === "post") {
                            data = aesUtil.decrypt(data.data.data, rsaUtil.decrypt(data.data.aesKey, window.jsPrivateKey));
                        }
                        //先获取明文aesKey，再用明文key去解密数据
                        fn.success(data, textStatus);
                    }
                });
                return _ajax(_opt);
            };
        </script>
    </head>

    <!-- thymeleaf内联js-->
    <head>
        <script th:inline="javascript">
            //项目根路径
            // ctx = /*[[@{/}]]*/'';
            ctx = [[${#request.getContextPath()}]];//应用路径
        </script>
    </head>
</html>